package com.winchain.chainbill.bill.biz.discount.cmd;

import com.shell.bee.acct.account.po.AcctCard;
import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.MoneyUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.contract.ContService;
import com.shell.bee.contract.ContTemplateHelper;
import com.shell.bee.contract.po.ContBas;
import com.shell.bee.contract.po.ContProt;
import com.shell.bee.contract.po.ContTmpl;
import com.shell.feign.PubProtocolFeign;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICommand;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.po.PubProtocol;
import com.winchain.chainbill.basic.charge.service.ValComService;
import com.winchain.chainbill.basic.charge.vo.ValComVo;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.esign.service.ContBasSnService;
import com.winchain.chainbill.tbill.biz.contract.keyword.EsignArtisanKeyword;
import com.winchain.chainbill.trade.service.BillAcctService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillSerFeeSponsorCmd implements ICommand<Bill<CbDct>> {
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private ContBasSnService contBasSnService;
	@Autowired
	private PubProtocolFeign pubProtocolFeign;
	@Autowired
	private ContService contService;
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private ValComService valComService;

	public boolean execute(CommandContext<Bill<CbDct>> context) throws Exception {
		CbRec tbRec = ((Bill) context.getSource()).getRec();
		CbDct tbDct = (CbDct) ((Bill) context.getSource()).getSource();
		if (tbDct == null) {
			return false;
		}
		if (!tbDct.getSellerId().equals(tbDct.getBuyerId())) {
			return false;
		}
		String bizId = tbDct.getBillId();
		String bizType = "51";
		String protTitle = "��������";
		String sponsorId = CommonDataUtil.getCurEntId();

		CusRec artisan = this.cusRecService.getArtisan(null);
		if (artisan == null) {
			throw new UnCheckMsgException("��������������������������������");
		}
		String acceptorId = artisan.getCusId();
		String protocolType = "artisan_proto";
		String grpId = tbRec.getGrpId();
		if (tbDct == null) {
			return false;
		}
		PubProtocol protocol = this.pubProtocolFeign.getPubProtocolByTypeAndGrpId(protocolType, grpId, null);
		List<ContTmpl> tmpls = this.contService.getContTmpl(bizType);
		List<ContProt> prots = null;
		CusRec cus = (CusRec) this.cusRecService.selectByKey(sponsorId);
		String contSn = this.contBasSnService.createContSn(cus.getGrpId());
		if ((protocol != null) && (tmpls != null) && (tmpls.size() > 0)) {
			prots = new ArrayList();
			String oriContent = protocol.getContent();
			String content = genContent(oriContent, tbDct, contSn, artisan, tbRec);
			ContTmpl tmpl = (ContTmpl) tmpls.get(0);
			ContProt prot = this.contService.genProtWithContent(tmpl.getTmplId(), protTitle, content, true);
			prots.add(prot);
		}
		ContBas cont = new ContBas();
		String contId = SystemUtil.getUUID();
		cont.setContId(contId);
		cont.setContSn(contSn);
		cont.setContTitle(protTitle);
		cont.setBizType(bizType);
		cont.setBizId(bizId);
		cont.setSponsor(sponsorId);
		cont.setAcceptor(acceptorId);
		cont.setContDate(SysDateUtil.getSystemDate());
		cont.setCreateTime(SysDateUtil.getSystemDateTime());
		cont.setContState("1");
		cont.setContProgress("0");
		this.contService.saveCont(cont, prots);
		return false;
	}

	private String genContent(String content, CbDct source, String contSn, CusRec artisan, CbRec cbRec) {
		EsignArtisanKeyword keyword = new EsignArtisanKeyword();
		keyword.setTechContSn(contSn);

		keyword.setTecTBillCode(cbRec.getBillCode());
		keyword.setTectDctAmt("��" + MoneyUtil.format(source.getDctAmt()));
		keyword.setTectSerFeeAmt("��" + MoneyUtil.format(source.getSerFee()));

		CusRec seller = (CusRec) this.cusRecService.selectByKey(CommonDataUtil.getCurEntId());
		keyword.setJfCompanyName(seller.getCusName());
		keyword.setJfEmail("");
		keyword.setJfHeader(seller.getLrName());
		keyword.setJfPhone(seller.getContactorPhone());

		keyword.setYfHeader(artisan.getLrName());
		keyword.setYfPhone(artisan.getContactorPhone());
		keyword.setYfCompanyName(artisan.getCusName());
		AcctCard acctCard = (AcctCard) this.billAcctService.getAcctBas(artisan.getCusId(), null, "00");
		if (acctCard != null) {
			keyword.setYfAcctNo(acctCard.getAcctNo());
			keyword.setYfBankName(acctCard.getHqFiNm());
			keyword.setYfAcctName(acctCard.getAcctNm());
		}
		ValComVo serviceFee = this.valComService.getDctValComVo(source.getFinId(), seller.getGrpId());
		if (serviceFee != null) {
			StringBuilder sb = new StringBuilder();
			String valType = serviceFee.getValType();
			if (serviceFee.getValStd() != null) {
				if ("1".equals(valType)) {
					sb.append(serviceFee.getValStd().stripTrailingZeros().toPlainString()).append("��");
				} else if ("2".equals(valType)) {
					sb.append(serviceFee.getValStd().stripTrailingZeros().toPlainString()).append("%");
				} else if ("3".equals(valType)) {
					sb.append(serviceFee.getValStd().stripTrailingZeros().toPlainString()).append("��");
				}
			} else if ((sb != null) && (sb.length() > 0)) {
				sb.append("0%");
			}
			if ((sb != null) && (sb.length() > 0)) {
				keyword.setTectSerFeeRate(sb.toString());
			}
		}
		keyword.setYfEmail("");
		String address = artisan.getProvince();
		if (!StringUtil.emptyAndNull(artisan.getCity())) {
			address = address + artisan.getCity();
		}
		keyword.setYfAddress(address + artisan.getAddress());
		keyword.setTechYearMonthDay(SysDateUtil.parseDate(SysDateUtil.getSystemDate(), "yyyyMMdd", "yyyy��MM��dd��"));

		return ContTemplateHelper.replaceContent(content, keyword);
	}
}